The TextField Component > FTextField.setMaskingFunction()

 

FTextField.setMaskingFunction()

Availability

Flash Player 6.

Usage

myTextField_txf.setMaskingFunction (fnc [,scope])

Parameters



fnc A String specifying the name of the function which will be used to mask user input.

scope An Object whose scope the masking function will be run within. This parameter if optional.


Returns

Nothing.

Description

Allows you to mask, transform and restrict user input into the text field. The passed in function will be passed the characters as they are typed. This function can then return what should be added to the text component, overriding the user's input.

Calling this method with no parameters deletes the masking function so that processor power will be saved.

The function used for the masking function will be passed three parameters:

char

The character that the user placed in the field.

index

The index of the new char in the field.

text

The current complete text within the text field.

Below is a masking function that takes all three parameters and traces their value.

myMaskingFunction(char, index, text)
{
	trace(char);
	trace(index);
	trace(text);
	
	return char;
}

The following masking function converts all user input into upper case characters.

myMaskingFunction(char, index, text)
{
	return char.toUpperCase();

}

If the maskingFunction does not return a value, the user's input will be discarded.

 

Example

The text_example2.fla example file contains an example of using this method.

The following code shows a masking class that takes two parameters, the first parameter is the character that will be replaced and the second is the character to replace it with.

function Mask(match,rep){ 
   this.match = match; 
   this.rep = rep; 
}


Mask.prototype.onMask = function(l){ 
   if(l!=this.match) return l; 
   else return this.rep; 
}


var myMask = new Mask(1,4); 
myTextField_txf.setMaskingFunction("onMask",myMask);

See also

FTextField.getMaskingFunction()